import tkinter as tk
import random
import math

WIDTH, HEIGHT = 800, 600


class Character:
    def __init__(self, canvas, x, y, color, is_fire):
        self.canvas = canvas
        self.x = x
        self.y = y
        self.color = color
        self.is_fire = is_fire
        self.size = 40
        self.weapon_angle = 0
        self.body_parts = []

    def draw(self):
        # Remove old body parts
        for part in self.body_parts:
            self.canvas.delete(part)
        self.body_parts = []

        # Head
        self.body_parts.append(
            self.canvas.create_oval(self.x - 20, self.y - 50, self.x + 20, self.y - 10, fill=self.color))
        # Body
        self.body_parts.append(
            self.canvas.create_line(self.x, self.y - 10, self.x, self.y + 40, fill=self.color, width=4))
        # Legs
        self.body_parts.append(
            self.canvas.create_line(self.x, self.y + 40, self.x - 20, self.y + 80, fill=self.color, width=4))
        self.body_parts.append(
            self.canvas.create_line(self.x, self.y + 40, self.x + 20, self.y + 80, fill=self.color, width=4))

        # Arms and weapon
        if self.is_fire:
            self.body_parts.append(
                self.canvas.create_line(self.x, self.y, self.x - 20, self.y + 20, fill=self.color, width=4))
            weapon_end = (self.x + 40 * math.cos(self.weapon_angle), self.y - 40 * math.sin(self.weapon_angle))
            self.body_parts.append(self.canvas.create_line(self.x, self.y, weapon_end, fill=self.color, width=4))
        else:
            self.body_parts.append(
                self.canvas.create_line(self.x, self.y, self.x + 20, self.y + 20, fill=self.color, width=4))
            weapon_end = (self.x - 40 * math.cos(self.weapon_angle), self.y - 40 * math.sin(self.weapon_angle))
            self.body_parts.append(self.canvas.create_line(self.x, self.y, weapon_end, fill=self.color, width=4))

    def update(self):
        self.weapon_angle += 0.1
        if self.weapon_angle > 2 * math.pi:
            self.weapon_angle = 0


class Particle:
    def __init__(self, canvas, x, y, color):
        self.canvas = canvas
        self.x = x
        self.y = y
        self.color = color
        self.size = random.randint(2, 6)
        self.speed = random.uniform(1, 3)
        self.angle = random.uniform(0, 2 * math.pi)
        self.lifetime = random.randint(30, 90)
        self.shape = canvas.create_oval(x - self.size, y - self.size, x + self.size, y + self.size, fill=color,
                                        outline="")

    def update(self):
        self.x += self.speed * math.cos(self.angle)
        self.y += self.speed * math.sin(self.angle)
        self.lifetime -= 1
        self.size = max(0, self.size - 0.05)
        self.canvas.coords(self.shape, self.x - self.size, self.y - self.size, self.x + self.size, self.y + self.size)


def create_particles(canvas, x, y, color):
    if random.random() < 0.3:
        return [Particle(canvas, x + random.randint(-20, 20), y - random.randint(0, 40), color)]
    return []


def update(canvas, fire_swordsman, ice_mage, particles):
    fire_swordsman.update()
    ice_mage.update()
    fire_swordsman.draw()
    ice_mage.draw()

    new_particles = (create_particles(canvas, fire_swordsman.x, fire_swordsman.y, "orange") +
                     create_particles(canvas, ice_mage.x, ice_mage.y, "cyan"))

    particles.extend(new_particles)

    for particle in particles[:]:
        particle.update()
        if particle.lifetime <= 0:
            canvas.delete(particle.shape)
            particles.remove(particle)

    canvas.after(33, update, canvas, fire_swordsman, ice_mage, particles)  # ~30 FPS


def main():
    root = tk.Tk()
    root.title("Fire Swordsman vs Ice Mage")

    canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
    canvas.pack()

    fire_swordsman = Character(canvas, 200, HEIGHT - 100, "red", True)
    ice_mage = Character(canvas, WIDTH - 200, HEIGHT - 100, "blue", False)

    particles = []

    update(canvas, fire_swordsman, ice_mage, particles)

    root.mainloop()


if __name__ == "__main__":
    main()